package com.example.servicetest;

import android.app.Activity;
import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.example.servicetest.ServiceBindTest.ContadorBinder;

public class ServiceBinderActivity extends Activity implements ServiceConnection {

	Contador contador;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.main);

		final TextView text = (TextView) findViewById(R.id.valorContador);

		Button btnBind = (Button) findViewById(R.id.btnBind);
		btnBind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// bindService
				Intent intent = new Intent("com.example.servicetest.SERVICE");
				bindService(intent, ServiceBinderActivity.this, BIND_AUTO_CREATE);
			}
		});

		Button btnUnbind = (Button) findViewById(R.id.btnUnbind);
		btnUnbind.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// unbindService
				unbindService(ServiceBinderActivity.this);
			}
		});

		Button btnGet = (Button) findViewById(R.id.btnLerContador);
		btnGet.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				// ler contador aqui
				int count = contador.get();
				text.setText("" + count);
			}
		});

		Button btnReset = (Button) findViewById(R.id.btnZerarContador);
		btnReset.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// zerar contador aqui
				contador.reset();
			}
		});
	}


	@Override
	public void onServiceConnected(ComponentName name, IBinder service) {
		ContadorBinder binder = (ContadorBinder) service;
		contador = binder.getInterface();
	}

	@Override
	public void onServiceDisconnected(ComponentName name) {
		contador = null;
	}
}
